package com.pctx.bibadevicemanager.service.impl;

import com.pctx.bibacommon.util.UuidUtil;
import com.pctx.bibadevicemanager.dao.UserDeviceRoleMapper;
import com.pctx.bibadevicemanager.pojo.UserDeviceRole;
import com.pctx.bibadevicemanager.pojo.UserDeviceRoleExample;
import com.pctx.bibadevicemanager.service.UserDeviceRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chason on 2018/3/1.11:15
 */
@Service("userDeviceRoleService")
public class UserDeviceRoleServiceImpl implements UserDeviceRoleService {
    @Autowired
    private UserDeviceRoleMapper userDeviceRoleMapper;

    @Override
    public boolean add(String userDeviceUuid, String roleUuids) throws Exception {
        if (StringUtils.isEmpty(userDeviceUuid) || StringUtils.isEmpty(roleUuids))
            return false;

        List<UserDeviceRole> rdrList = new ArrayList<UserDeviceRole>();
        for (String roleUuid : roleUuids.split(",")) {
            UserDeviceRole udr = new UserDeviceRole();
            udr.setUuid(UuidUtil.randomUUID());
            udr.setUserdeviceUuid(userDeviceUuid);
            udr.setRoleUuid(roleUuid);
            rdrList.add(udr);
        }

        return userDeviceRoleMapper.insertUserDeviceRoles(rdrList) > 0;
    }

    @Override
    public boolean delByUserDeviceUuid(String userDeviceUuid) throws Exception {
        if (StringUtils.isEmpty(userDeviceUuid))
            return false;

        UserDeviceRoleExample example = new UserDeviceRoleExample();
        UserDeviceRoleExample.Criteria criteria = example.createCriteria();
        criteria.andUserdeviceUuidEqualTo(userDeviceUuid);

        return userDeviceRoleMapper.deleteByExample(example) > 0;
    }

}
